package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketAddress;
import com.cskaoyan.market.db.domain.MarketAddressExample;
import com.cskaoyan.market.db.domain.MarketSearchHistory;
import com.cskaoyan.market.db.domain.MarketSearchHistoryExample;
import com.cskaoyan.market.db.mapper.MarketAddressMapper;
import com.cskaoyan.market.db.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminHistoryServiceImpl implements AdminHistoryService {
    @Override
    public List<MarketSearchHistory> list(final String page, final String limit, final String sort, final String order, final String userId, final String keyword) {
        SqlSession session = MybatisUtils.getSession();
        MarketSearchHistoryMapper marketSearchHistoryMapper = session.getMapper(MarketSearchHistoryMapper.class);
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();

        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }

        marketSearchHistoryExample.setOrderByClause(sort + " " + order);

        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketSearchHistory> marketSearchHistoryList = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);
        session.commit();
        session.close();
        return marketSearchHistoryList;
    }
}
